package com.dubbo.utils;

public class RuntimeUtils {

    public static boolean releasePort(int port) {

        try {

            if (getOS() == OS.LINUX) {
                String command = "kill -9 `lsof -i:%s |grep -v \"PID\"|awk '{print$2}'`";
                command = String.format(command, port);
                Process exec = Runtime.getRuntime().exec(command);
                return exec.exitValue() == 0;
            }

            return false;

        } catch (Throwable e) {
            return false;
        }
    }

    public static OS getOS() {
        String os = System.getProperty("os.name").toLowerCase();

        if (os.toLowerCase().contains("linux")) {
            return OS.LINUX;
        }

        if (os.toLowerCase().contains("windows")) {
            return OS.WINDOWS;
        }

        return OS.OTHER;
    }

    public enum OS {
        LINUX, WINDOWS, OTHER
    }

}
